package com.example.baijiebrower.settings;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.baijiebrower.MainActivity;
import com.example.baijiebrower.R;
import com.example.baijiebrower.tools.FormData;
import com.example.baijiebrower.tools.Permissions;

public class Settings extends AppCompatActivity {

    private RadioGroup group;
    private FormData formData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initPermission(this);
        init();
        initRadio();
        initGroup();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dumpActivity();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initPermission(Activity activity) {
        String[] permissions = {
                "android.permission.INTERNET",
                "android.permission.WRITE_EXTERNAL_STORAGE",
                "android.permission.READ_EXTERNAL_STORAGE",
        };
        Permissions.checkPermission(activity,permissions);
    }

    private void init(){
        group = findViewById(R.id.radio_group);
        String[] headers = getResources().getStringArray(R.array.header);
        for(int i = 0; i < headers.length; i++){
            RadioButton radioButton = new RadioButton(this);
            radioButton.setText(headers[i]);
            radioButton.setTag(headers[i]);
            radioButton.setId(View.generateViewId());
            group.addView(radioButton);
        }
    }

    private void initRadio(){
        formData = new FormData();
        setSelectedRadioButton(formData.getHeader(this));
    }

    private void setSelectedRadioButton(String headerText) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) group.getChildAt(i);
            if (headerText.equals(radioButton.getTag())) {
                radioButton.setChecked(true);
                break;
            }
        }
    }

    private void initGroup(){
        group.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton selectedRadioButton = findViewById(checkedId);
            if (selectedRadioButton != null) {
                String selectedText = selectedRadioButton.getText().toString();
                formData.saveHeader(this, selectedText);
                formData.saveSetHttp(this);
                dumpActivity();
            }
        });
    }

    private void dumpActivity(){
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}